<template>
  <el-row>
    <el-col :span="8">
      <el-card shadow="hover" class="h100">
        <h3 class="mtb0">抽题编辑</h3>
        <div v-for="item in modelForm.quantityList" :key="item.classificationId" class="mt10">
          <div>
            <span class="label">{{ item.name }}</span>
            <el-input-number v-model="item.quantity" :min="0" :max="item.maxQuantity"
              :label="item.name"></el-input-number>
          </div>
        </div>
      </el-card>
    </el-col>
    <el-col :span="16">
      <el-card shadow="hover" class="h100">
        <h3 class="mtb0">试卷编辑</h3>
        <el-form :rules="rules" ref="form" :model="modelForm">
          <el-form-item prop="name" label="试卷名称" class="mt10">
            <el-input v-model="modelForm.name" auto-complete="off" placeholder="请输入试卷名称">
            </el-input>
          </el-form-item>
          <el-form-item label="考试要求">
            <el-input v-model="modelForm.note" auto-complete="off" type="textarea" placeholder="请输入考试要求">
            </el-input>
          </el-form-item>
          <el-form-item label="阅卷角色">
            <el-select v-model="modelForm.checkRoleId" style="width: 100%" placeholder="请选择阅卷角色">
              <el-option v-for="item in roleList" :key="item.id" :label="item.roleName" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="passPercent" label="合格分">
            <el-input v-model="modelForm.passPercent" auto-complete="off" placeholder="请输入合格分">
              <template #append>
                <div>
                  %
                </div>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="maxTime" label="考试时长">
            <el-input v-model="modelForm.maxTime" auto-complete="off" placeholder="请输入考试时长">
              <template #append>
                <div>
                  分钟
                </div>
              </template>
            </el-input>
          </el-form-item>
          <div class="flex flex-center">
            <el-button type="primary" @click="submitHandle">提交</el-button>
          </div>
        </el-form>
      </el-card>
    </el-col>
  </el-row>
</template>

<script>
import { getDetail, update } from '@/api/position/examRule';
import { getList } from '@/api/system/user';
import { getExpertList } from '@/api/position/expert';

export default {
  props: {
    tabsData: Array,
  },
  data() {
    return {
      modelForm: {
        name: '',
        checkRoleId: "",
        note: '',
        passPercent: 60,
        maxTime: 0,
        id: '',
        status: 1,
        isDeleted: 0,
        tenantId: '000000',
        questionRule: '',
        positionInstanceId: '',
        quantityList: [],
      },
      rules: {
        name: [{ required: true, trigger: 'blur', message: '请输入试卷名称' }],
        passPercent: [{ required: true, trigger: 'blur', message: '请输入合格分数' }],
        maxTime: [{ required: true, trigger: 'blur', message: '请输入考试时长' }],
      },
      roleList: [],
    };
  },
  created() {
    getExpertList().then(res => {
      console.log(res.data.data);
      this.roleList = res.data.data;
    })
  },
  methods: {
    async getDetail(id) {
      if (!id) {
        return;
      }
      const res = await getDetail(id);
      const data = res.data.data;
      this.modelForm = {
        id: data.id,
        status: data.status,
        isDeleted: data.isDeleted,
        tenantId: data.tenantId,
        name: data.name,
        questionRule: data.questionRule,
        positionInstanceId: data.positionInstanceId,
        passPercent: data.passPercent,
        maxTime: data.maxTime,
        certificateIds: data.certificateIds,
        excusedRule: data.excusedRule,
        code: data.code,
        note: data.note,
        checkRoleId: data.checkRoleId,
        quantityList: data.quantityList
      };
    },
    resetHandle() {
      this.$refs.form.resetFields();
    },
    submitHandle() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const form = JSON.parse(JSON.stringify(this.modelForm));
          form.passPercent = Number(form.passPercent);
          form.maxTime = Number(form.maxTime);
          let checkRoleName = this.roleList.find(item => form.checkRoleId == item.id)
          form.checkRoleName = checkRoleName ? checkRoleName.roleName : "";
          update(form).then(res => {
            this.$message.success('提交成功');
          });
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.label {
  display: inline-block;
  width: 90px;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 14px;
  white-space: nowrap;
}
</style>
